package com.ali.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * ioc
 * @author jack.wu
 * @version 1.0
 * @date 2020/8/19
 */
public class MyApplicationContext implements ApplicationContextAware {

    public static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext ioc) throws BeansException {
        applicationContext = ioc;
    }

    /**
     * 根据bean的id来查找对象
     *
     * @param id beanId
     * @return 对象
     */
    public static Object getBeanById(String id) {
        return applicationContext.getBean(id);
    }

    /**
     * 根据bean的类型来查找对象
     *
     * @param clazz bean类型
     * @return 对象
     */
    public static Object getBeanByClass(Class clazz) {
        return applicationContext.getBean(clazz);
    }

}
